﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Web.Optimization;

namespace McKearney.BundlingTweaks
{
    public class NonMinifyingBundleBuilder : IBundleBuilder
    {
        readonly IBundleBuilder _impl;
        public NonMinifyingBundleBuilder(IBundleBuilder impl)
        {
            if (impl == null)
            {
                throw new ArgumentNullException("impl");
            }
            _impl = impl;
        }

        #region IBundleBuilder Members

        public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable<System.IO.FileInfo> files)
        {
            List<FileInfo> debugVersions = new List<FileInfo>();

            // strip "min" if the file exists
            foreach (var inFile in files)
            {
                FileInfo newFileToUse = GetDebugReplacement(inFile);

                if (newFileToUse == null)
                {
                    // use the original
                    if (TweakSettings.EnableTraceOutput == true)
                    {
                        Trace.TraceInformation("Using Original File for {0}", inFile.Name);
                    }
                    debugVersions.Add(inFile);
                }
                else
                {
                    if (TweakSettings.EnableTraceOutput == true)
                    {
                        Trace.TraceInformation("Replacing {0} with {1}", inFile.Name, newFileToUse.Name);
                    }
                    debugVersions.Add(newFileToUse);
                }
            }

            // proxy it
            return _impl.BuildBundleContent(bundle, context, debugVersions);
        }

        private FileInfo GetDebugReplacement(FileInfo inFile)
        {
            FileInfo newFileToUse = null;

            string fullName = inFile.FullName;
            string ext = inFile.Extension;

            string withoutExt = fullName.Substring(0, fullName.Length - ext.Length);

            newFileToUse = null;
            foreach (var replacement in TweakSettings.ExtensionReplacements)
            {
                string key = replacement.Key;
                string val = replacement.Value;

                if (key == val)
                {
                    // useless
                    continue;
                }

                string newName = withoutExt;
                bool check = false;

                // we're removing 'key' first if it's there
                if (string.IsNullOrEmpty(key) == false
                    && withoutExt.EndsWith(key) == true)
                {
                    // see if there's a file without "val" in the name
                    newName = withoutExt.Substring(0, withoutExt.Length - key.Length);
                    check = true;
                }

                // if we're adding somethign back in, do it now.
                if (string.IsNullOrEmpty(val) == false)
                {
                    // this is additive (we're just inserting 'val' before the extension)
                    // tack the basic extension back on
                    newName = string.Format("{0}{1}", newName, val);
                    check = true;
                }

                if (check == true)
                {
                    // tack the original extension back on
                    newName += ext;

                    FileInfo newFile = new FileInfo(newName);
                    if (newFile.Exists == true)
                    {
                        // we found it, break;
                        newFileToUse = newFile;
                        break;
                    }
                }
            }
            return newFileToUse;
        }

        #endregion
    }
}
